#!/usr/bin/env python
# $Id$
#
# This file is part of Phennui.
#
# Phennui is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Phennui is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Phennui.  If not, see <http://www.gnu.org/licenses/>.

import pygtk
pygtk.require('2.0')
import gtk, gobject
import os
import common
import fileeditor

class preferencesdialog:
       
    def __init__(self, ui):       
        try:
            self.config = fileeditor.FileEditor('config.py')   
        except:
            self.create_default_config()
            self.config = fileeditor.FileEditor('config.py') 
       
        # Store the gui in a var.
        self.ui = ui
        
        # Fill the gui with the values from the config.
        self.set_values()
        
    def event_response(self, dialog, response):
        self.updatewarnings()
        if response in [gtk.RESPONSE_OK, gtk.RESPONSE_APPLY]:
            self.get_values()
            self.save_config()
            if response == gtk.RESPONSE_OK:
                dialog.hide()
        else:
            self.set_values()
            dialog.hide()
        
    def set_values(self):
        self.config = fileeditor.FileEditor('config.py')
               
        # General.
        # Phenny Executable.
        self.ui.get_object('preferencesdialog_general_exec_chooser').set_filename(self.config.phenny_exec)
        
        # Phenny Config Dir.
        self.ui.get_object('preferencesdialog_general_configdir_chooser').set_filename(self.config.configdir)
        
        # Display.      
        # Group by.
        self.ui.get_object('preferencesdialog_display_groupby_' + (self.config.groupby in common.groupby_cols and self.config.groupby or 'none')).set_active(True)
        
        # Columns.
        for name, value in self.config.columns:
            self.ui.get_object('preferencesdialog_display_columns_' + name).set_active(value)
            
        # Show Terminals
        self.ui.get_object('preferencesdialog_display_terminals').set_active(self.config.show_terminals)
        
        
        # Hide the warnings.
        self.ui.get_object('preferencesdialog_warnings').hide()
        
    def get_values(self):
        # General.
        # Phenny Executable.
        self.config.phenny_exec = self.ui.get_object('preferencesdialog_general_exec_chooser').get_filename()
        
        # Phenny Config Dir.
        self.config.configdir = self.ui.get_object('preferencesdialog_general_configdir_chooser').get_filename()
                
        # Display.      
        # Group by.
        for name in common.groupby_cols:
            if self.ui.get_object('preferencesdialog_display_groupby_' + name).get_active():
                self.config.groupby = name
                break
        
        # Columns.
        for i, (name, value) in enumerate(self.config.columns):
            self.config.columns[i] = (name, self.ui.get_object('preferencesdialog_display_columns_' + name).get_active())
        
        self.config.show_terminals = self.ui.get_object('preferencesdialog_display_terminals').get_active()
        
    def updatewarnings(self, button = None):
        if self.config.configdir != self.ui.get_object('preferencesdialog_general_configdir_chooser').get_filename():
            self.ui.get_object('preferencesdialog_warnings').set_label('WARNING: Changing the config dialog requires a restart.')
            self.ui.get_object('preferencesdialog_warnings').show()
        else:
            self.ui.get_object('preferencesdialog_warnings').set_label('')
            self.ui.get_object('preferencesdialog_warnings').hide()
               
    def save_config(self):   
        f = open('config.py', 'w')
        f.write(str(self.config))
        f.close()
    
    @staticmethod
    def create_default_config():
        f = open('config.py', 'w')
        f.write("""phenny_exec = ''
configdir = '%s'
columns = [('name', True), ('server', True), ('nick', True), ('owner', True), ('state', True)]
groupby = 'none'
show_terminals = False""" % os.path.join(os.path.expanduser('~'), '.phenny'))
        f.close()
        
    def run(self):
        self.set_values()
        self.ui.get_object('preferencesdialog').run()
        
